#ifndef __HTTPREQUEST_H__
#define __HTTPREQUEST_H__

#include <unordered_map>
#include <string>
#include <regex>

#include "Buffer.h"

namespace NetWork
{
    // http 请求报文解析
    /*
        +--------------------------------------------------------+
        | 请求方法 | 空格 | URL | 空格 | 协议版本 | 回车符 | 换行符 |   (请求行)
        +-------------------------------------+------------------+
        | 头部字段名 | : | 值 | 回车符 | 换行符 |   (请求头 headers)
        |                ...                  |
        +-------------------------------------+
        | 回车符 | 换行符 |
        +----------------+
        | .....data......|  (请求体 body)
        +----------------+
    */
    class HttpRequest
    {
    private:
        std::unordered_map<std::string, std::string> Headers;
        Buffer RequestData;
        Buffer Body;
        std::string Method;
        std::string Url;
        std::string ProtocolVersion;

        enum class State {
            Line,
            Headers,
            Body,
            Finish
        } ParseState;

    public:
        HttpRequest();
        HttpRequest(Buffer&& requestData);
        ~HttpRequest() = default;

    public:
        void Parse(const void* requestData, size_t len);

        inline const std::unordered_map<std::string, std::string>& GetHeaders() const;
        inline const Buffer& GetBody() const;
        inline const std::string& GetMethod() const;
        inline const std::string& GetUrl() const;
        inline const std::string& GetProtocolVersion() const;


    private:
        void Parse();
        void ParseLine();
        void ParseHeaders();
        void ParseBody();
    };
    
} // namespace NetWork

#endif
